package com.wwg.market.ui.code.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Widget;
import com.mobileares.midp.mui.client.form.UFishFlowForm;
import com.mobileares.midp.widgets.client.button.CButton;
import com.mobileares.midp.widgets.client.pop.PopPanel;
import com.mobileares.midp.widgets.client.textbox2.*;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.wwfish.gwt.model.client.IModelCallback;
import com.wwfish.gwt.model.client.ISaveAndUpdateModelProvider;
import com.wwfish.gwt.model.client.impl.FishModelManager;
import com.wwfish.gwt.model.client.impl.FormModel;
import com.wwfish.gwt.model.client.impl.ListModel;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.service.dto.GeneralCodeDto;
import com.wwg.market.ui.service.interfaces.CodeService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author gaoyang
 * @version $Id: $
 * @date 11-12-20 上午12:56
 */
public class CodeForm {
    private UFishFlowForm<GeneralCodeDto> form;
    private PopPanel pop;
    private ListModel<GeneralCodeDto> listModel;

    public CodeForm() {
        pop = new PopPanel(true);
        init();
        initFormModel();
    }

    public void setModel(ListModel<GeneralCodeDto> model) {
        this.listModel = model;
    }

    private void init() {
        form = new UFishFlowForm<GeneralCodeDto>(1);
        TextBoxLabel text = new TextBoxLabel("代码名称:");
        TextBoxLabel code = new TextBoxLabel("代码值:");
        TextAreaLabel description = new TextAreaLabel("描述:");


        form.addElement(text, "name", new ILabelValidator[]{LabelValidatorFactory.require()});
        form.addElement(code, "code", new ILabelValidator[]{LabelValidatorFactory.require(), getCodeUniqueValidator()});
        form.addElement(description, "description");
        pop.addBodyWidget(form);

        CButton confirm = new CButton("确定");
        confirm.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                form.getFishModel().saveAndUpdate(form.getFishWidget());
            }
        });
        CButton cancel = new CButton("取消");
        cancel.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                pop.hidden();
            }
        });
        pop.addFootButton(confirm);
        pop.addFootButton(cancel);

    }

    public void show(GeneralCodeDto dto) {
        form.getFishModel().load(dto);
        pop.show();
    }

    public void hidden() {
        pop.hidden();
    }

    public void setTitle(String text) {
        pop.setHeadText(text);
    }

    private void initFormModel() {
        FormModel model = (FormModel) FishModelManager.createTempModel(FishModelManager.TYPE_FORM);
        model.setSaveAndUpdateProvider(new ISaveAndUpdateModelProvider<GeneralCodeDto>() {
            public void saveAndUpdate(final GeneralCodeDto value, final IModelCallback callback) {
                Utils.invoke(CodeService.class.getName(), "saveOrUpdateCode", new Object[]{value}, new LoadingAsyncCallback() {
                    @Override
                    protected void success(Object o) {
                        hidden();
                        callback.setFishModel(o);
                        if (value.getId() == null)
                            listModel.addItem(0, (GeneralCodeDto) o);
                        else
                            listModel.updateItem(value, (GeneralCodeDto) o);

                    }
                });
            }
        });
        form.setFishModel(model);

    }

    public ILabelValidator getCodeUniqueValidator() {
        return new ILabelValidator() {
            public boolean fire(Widget ui) {
                final BaseLabelText box = (BaseLabelText) ui;
                boolean flag = true;
//                final boolean finalFlag = flag;
//                Utils.invoke(CodeService.class.getName(), "checkCodeUnique", new Object[]{box.getValue()}, new LoadingAsyncCallback() {
//                    @Override
//                    protected void success(Object o) {
//                        LabelValidatorFactory.action(box, finalFlag, "代码不唯一");
//                    }
//                });
                List<GeneralCodeDto> codes = new ArrayList<GeneralCodeDto>(listModel.getModel());  //todo:某一类别下的代码值唯一
                codes.remove(form.getFishModel().getModel());

                flag = checkCodeUnique((String) box.getValue(), codes);
                LabelValidatorFactory.action(box, flag, "代码不唯一");
                return flag;
            }
        };
    }

    private boolean checkCodeUnique(String code, List<GeneralCodeDto> list) {
        boolean result = true;
        if (code != null) {
            for (int i = 0; i < list.size(); i++) {
                GeneralCodeDto codeDto = list.get(i);
                if (code.trim().equals(codeDto.getCode())) {
                    result = false;
                    break;
                }
            }
        }
        return result;
    }
}
